You should do these problems on paper or mentally first, and only THEN check the solution.
Context-free grammars, parse trees, and ambiguity.
For the following languages, give the strings indicated. You may leave off quote marks for simplicity. The ordering of the strings is just to simplify checking solutions, and is not an essential part of the problem (don't fret about it).
7.1. The language { ak b ak | for k ≥ 1 } (just give the first 5 strings).
7.2. The language generated by the grammar (list all strings of length ≤ 5):
S := a S a S := b S b S := c S := dShow Solution
7.3. The language generated by the grammar (list all strings of length ≤ 4):
S := A B A := a A A := c B := B b B := dShow Solution
7.4. The language generated by the grammar (list all strings of length ≤ 5):
S := A A := a A a A := B B := b B b B := cShow Solution
7.5. The language generated by the grammar (list all strings of length ≤ 7):
S := A S := B A := a A bb A := a B := aa B b B := bShow Solution
Problem 7.6 -- 7.9 are not relevant for Midterm 2 in Spring 2019.
7.6. Give a CFG corresponding to the regular expression (a (bb|cc) b)* a
7.7. For which of the regular expressions given in Part 1 would it be possible to construct a CFG generating the same language?
7.8. Give a regular expression representing the same language as the CFG in problem 2.3.
7.9. For which of the grammars in problems 2.2 - 2.5 would it be possible to find a regular expression representing the same language?
The following problem is designed to go with material on finite-state automata, but can be solved without it!
7.10. Give a CFG generating the language over {a,b}* with an even number of a's and an odd number of b's Hint: draw an automaton first, then write a grammar which has a non-terminal for each state; you may need to use erasing rules of the form A := ε.
For the following grammars and string s, give (a) a parse tree (b) a left-most derivation, and (c) a right-most derivation.
7.11.
S := A B A := a A A := c B := B b B := dString s = aacdb
7.12.
S := E E := a E := [ L ] E := [] L := E , L L := EString s = [ a, [ a ] ]
7.13.
S := E E := E + T E := T T := T * F T := F F := - F F := F1 F1 := F2 ** F1 F1 := F2 F2 := aString s = a + - a ** a
7.14.
S := E E := E + T E := T T := T * F T := F F := - F F := aString s = a * a + -a
For the following CFGs, state whether or not they are ambiguous, and if ambiguous, give two different left-most derivations for some string OR two different parse trees.
7.15.
S := A A := a A A := B B := a B B := aShow Solution
7.16.
S := A A := a A b A := a A A := bShow Solution
7.17. (This grammar attempts to generate the language with an equal number of a's and b's.)
S := A A := a A b A := b A a A := A A A := a b A := b a
7.18. (The language of matching parentheses.)
S := A A := ( A ) A := A A A := ()
Answer the following two questions.
7.19. Write a CFG that represents the same language as the CFG in problem 7.15, but is NOT ambiguous.
7.20. Write a CFG that represents the same language as the CFG in problem 7.18, but is NOT ambiguous.